<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备编号" prop="terId">
        <el-input v-model="queryParams.terId" placeholder="请输入设备编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="车牌号码" prop="carNo">
        <el-input v-model="queryParams.carNo" placeholder="请输入车牌号码" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="车牌颜色" prop="carNoColor">
        <el-select v-model="queryParams.carNoColor" placeholder="请选择车牌颜色" clearable size="small">
          <el-option v-for="dict in carNoColorOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="terType">
        <el-select v-model="queryParams.terType" placeholder="请选择设备类型" clearable size="small">
          <el-option v-for="dict in terTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="SIM卡号" prop="simNo">
        <el-input v-model="queryParams.simNo" placeholder="请输入SIM卡号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="车辆类型" prop="vehicleType">
        <el-select v-model="queryParams.vehicleType" placeholder="请选择车辆类型" clearable size="small">
          <el-option v-for="dict in vehicleTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="地图标注名" prop="mark">
        <el-input v-model="queryParams.mark" placeholder="请输入地图标注名" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['baseCarInfo:info:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['baseCarInfo:info:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['baseCarInfo:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['baseCarInfo:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备编号" align="center" prop="terId" />
      <el-table-column label="车牌号码" align="center" prop="carNo" />
      <el-table-column label="车牌颜色" align="center" prop="carNoColor" :formatter="carNoColorFormat" />
      <el-table-column label="设备类型" align="center" prop="terType" :formatter="terTypeFormat" />
      <el-table-column label="SIM卡号" align="center" prop="simNo" />
      <el-table-column label="车辆类型" align="center" prop="vehicleType" :formatter="vehicleTypeFormat" />
      <el-table-column label="人员名称" align="center" prop="owersName" />
      <el-table-column label="人员电话" align="center" prop="owersTel" />
      <el-table-column label="地图标注名" align="center" prop="mark" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="车辆名称" align="center" prop="carName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['baseCarInfo:info:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['baseCarInfo:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改车辆信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="form_default">
        <div class="form_item_head">设备信息</div>
        <el-form-item label="设备编号" prop="terId">
          <el-input v-model="form.terId" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备类型" prop="terType">
          <el-select v-model="form.terType" placeholder="请选择设备类型">
            <el-option v-for="dict in terTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <div class="form_item_head">车辆信息</div>
        <el-form-item label="车牌号码" prop="carNo">
          <el-input v-model="form.carNo" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="车辆名称" prop="carName">
          <el-input v-model="form.carName" placeholder="请输入车辆名称" />
        </el-form-item>
        <el-form-item label="车牌颜色" prop="carNoColor">
          <el-select v-model="form.carNoColor" placeholder="请选择车牌颜色">
            <el-option v-for="dict in carNoColorOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际车辆" prop="remarkName">
          <el-input v-model="form.remarkName" placeholder="请输入实际车辆" />
        </el-form-item>

        <el-form-item label="SIM卡号" prop="simNo">
          <el-input v-model="form.simNo" placeholder="请输入SIM卡号" />
        </el-form-item>
        <el-form-item label="车辆类型" prop="vehicleType">
          <el-select v-model="form.vehicleType" placeholder="请选择车辆类型">
            <el-option v-for="dict in vehicleTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆区号" prop="vehicleZcode">
          <el-input v-model="form.vehicleZcode" placeholder="请输入车辆区号" />
        </el-form-item>
        <el-form-item label="地图标注名" prop="mark">
          <el-input v-model="form.mark" placeholder="请输入地图标注名" />
        </el-form-item>
        <div class="form_item_head">人员信息</div>
        <el-form-item label="人员名称" prop="owersName">
          <el-input v-model="form.owersName" placeholder="请输入人员名称" />
        </el-form-item>
        <el-form-item label="人员电话" prop="owersTel">
          <el-input v-model="form.owersTel" placeholder="请输入人员电话" />
        </el-form-item>

        <el-form-item label="安装人员" prop="installMan">
          <el-input v-model="form.installMan" placeholder="请输入安装人员" />
        </el-form-item>
        <el-form-item label="安装时间" prop="installTime">
          <el-date-picker clearable size="small" v-model="form.installTime" type="date" value-format="yyyy-MM-dd" placeholder="选择安装时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="备注" prop="remark" class="width100">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  exportInfo,
} from "@/api/baseCarInfo/info";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 车牌颜色字典
      carNoColorOptions: [],
      // 设备类型字典
      terTypeOptions: [],
      // 车辆类型字典
      vehicleTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        terId: null,
        carNo: null,
        carNoColor: null,
        terType: null,
        simNo: null,
        vehicleType: null,
        vehicleZcode: null,
        owersName: null,
        owersTel: null,
        deptId: null,
        isSure: null,
        feeVaild: null,
        feeBegin: null,
        feeEnd: null,
        installMan: null,
        installTime: null,
        driver1: null,
        driver2: null,
        driverLine: null,
        initMile: null,
        initMileTime: null,
        carIcoType: null,
        inUse: null,
        mark: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        carName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        terId: [
          { required: true, message: "设备编号不能为空", trigger: "blur" },
        ],
        terType: [
          { required: true, message: "设备类型不能为空", trigger: "change" },
        ],
        carNo: [
          { required: true, message: "车牌号码不能为空", trigger: "blur" },
        ],
        carName: [
          { required: true, message: "车辆名称不能为空", trigger: "blur" },
        ],
        remarkName: [
          { required: true, message: "实际车辆不能为空", trigger: "blur" },
        ],
        simNo: [
          { required: true, message: "SIM卡号不能为空", trigger: "blur" },
        ],
        owersName: [
          { required: true, message: "人员名称不能为空", trigger: "blur" },
        ],
        installMan: [
          { required: true, message: "安装人员不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("car_no_color").then((response) => {
      this.carNoColorOptions = response.data;
    });
    this.getDicts("ter_type").then((response) => {
      this.terTypeOptions = response.data;
    });
    this.getDicts("vehicle_type").then((response) => {
      this.vehicleTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询车辆信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 车牌颜色字典翻译
    carNoColorFormat(row, column) {
      return this.selectDictLabel(this.carNoColorOptions, row.carNoColor);
    },
    // 设备类型字典翻译
    terTypeFormat(row, column) {
      return this.selectDictLabel(this.terTypeOptions, row.terType);
    },
    // 车辆类型字典翻译
    vehicleTypeFormat(row, column) {
      return this.selectDictLabel(this.vehicleTypeOptions, row.vehicleType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carId: null,
        terId: null,
        carNo: null,
        carNoColor: null,
        terType: null,
        simNo: null,
        vehicleType: null,
        vehicleZcode: null,
        owersName: null,
        owersTel: null,
        deptId: null,
        isSure: null,
        feeVaild: null,
        feeBegin: null,
        feeEnd: null,
        installMan: null,
        installTime: null,
        driver1: null,
        driver2: null,
        driverLine: null,
        initMile: null,
        initMileTime: null,
        carIcoType: null,
        inUse: null,
        mark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        carName: null,
        remarkName: null,
        useStatus: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.carId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carId = row.carId || this.ids;
      getInfo(carId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.carId != null) {
            updateInfo(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carIds = row.carId || this.ids;
      this.$confirm(
        '是否确认删除车辆信息编号为"' + carIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInfo(carIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有车辆信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
